<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui">
	<meta name="apple-mobile-web-app-capable" content="yes" />
	<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
	<meta name="apple-mobile-web-app-title" content="阿福易购">
	<meta content="telephone=no" name="format-detection" />
	<meta content="email=no" name="format-detection" />
	<!-- uc、qq浏览器 -->
	<meta name="screen-orientation" content="portrait">
	<meta name="full-screen" content="yes">
	<meta name="browsermode" content="application">
	<meta name="x5-orientation" content="portrait">
	<meta name="x5-fullscreen" content="true">
	<meta name="x5-page-mode" content="app">
	<meta name="HandheldFriendly" content="true">
	<meta name="MobileOptimized" content="320">
	<meta name="csrf-token" content="{{ csrf_token() }}" />
	<meta name="keywords" content="{{$SEO['keywords']}}"/>
	<meta name="description" content="{{$SEO['description']}}"/>
	<title>我的订单_{{$SEO['title']}}</title>
    <link rel="stylesheet" href="{{ asset('/build/css/app.min.css') }}">
</head>
<body ontouchstart="">

@inject('request','Illuminate\Http\Request')
@inject('pictureService','App\Interfaces\PictureService')
@inject('orderService','App\Interfaces\OrderService')
@inject('refundGoodsService','App\Interfaces\RefundGoodsService')

<header class="ui-header ui-header-stable    af-header">
    <ul class="ui-row-flex">
        <li class="ui-col ui-flex ui-flex-pack-start">
            <i data-href="/user" class="ui-icon-return af-icon-home"></i>
        </li>
        <li class="ui-col ui-flex ui-flex-pack-center">
            <h1>我的订单</h1>
        </li>
        <li class="ui-col ui-flex ui-flex-pack-end">

        </li>
    </ul>
</header>

<div class="ui-container" >
    <div class="ui-tab ui-my-order-tab">
	    <section class="J-filter-scroll ui-scroll-menu af-filter-scroller">
	        <ul class="ui-scroll-menu-nav scroll-sm">
	            <li data-href="/my/order" @if(!@$request->condations['order_status'] && !@$request->condations['refund_goods']) class="current" @endif>全部</li>
	            <li data-href="/my/order?condations[order_status]=1" @if(@$request->condations['order_status'] && @$request->condations['order_status']=='1') class="current" @endif>待付款</li>
	            <li data-href="/my/order?condations[order_status]=2" @if(@$request->condations['order_status'] && @$request->condations['order_status']=='2') class="current" @endif>待发货</li>
	            <li data-href="/my/order?condations[order_status]=3" @if(@$request->condations['order_status'] && @$request->condations['order_status']=='3') class="current" @endif>待收货</li>
	            <li data-href="/my/order?condations[order_status]=4" @if(@$request->condations['order_status'] && @$request->condations['order_status']=='4') class="current" @endif>待评价</li>
	        </ul>
	    </section>

        @if($orders->total())

			<?php $status_arr = ['0'=>'全部订单', '1'=>'待付款', '2'=>'待发货', '3'=>'待收货','4'=>'待评价'] ?>

	            <div id="listWrap">

	            @foreach($orders as $key=>$order)

		            <?php

			            $pay_type = 1;

			            switch ($order->pay_type) {

			            	case 'alipay':

			            		$pay_type = 1;

			            		break;

			            	case 'wxpay':

			            		$pay_type = 3;

			            		break;

			            	case 'pinganpay':

			            		$pay_type = 4;

			            		break;

			            	default:

			            		$pay_type = 1;

			            		break;
			            }
		            ?>
	                <div class="ui-my-order ui-border-tb">
	                    <div class="ui-my-order-header">
	                        <div class="title">
	                            订单号：{{$order->order_sn}}
	                        </div>
	                        <div class="del">
	                            @if($order->order_status==4 || $order->order_status==5)
	                            <i class="ui-icon-delete J-delete" data-id="{{$order->order_id}}"></i>
	                            @endif
	                        </div>
	                        <div class="status">
	                                @if($order->order_status==1)
	                                <span class="text-color-7">
	                                待支付
	                                </span><br>

	                                @elseif($order->order_status==2)
	                                <span class="text-color-7">
	                                待发货
	                                </span><br>

	                                @elseif($order->order_status==3)
	                                <span class="text-color-1">待收货</span><br>
                                    </span><br>

                                    @elseif($order->order_status==4)
	                                <span class="text-color-7">
                                       @if($order->comment_status==0)
                                        待评价
                                       @elseif($order->comment_status==1)
                                        已评价
                                       @endif
	                                </span><br>
                                    

	                            @elseif($order->order_status==5)
	                                <span class="text-color-7">
	                                已取消
	                                </span><br>
                                @elseif($order->order_status==6)
                                    <span class="text-color-7">
                                    已退款
                                    </span><br>
	                            @endif
	                        </div>
	                    </div>

	                    <div class="ui-my-order-content">
	                        <ul class="ui-list ui-border-tb" onclick="window.location.href='/my/order/{{$order->order_id}}'">

			                    <?php 

                                    /*$goodsList=$orderService->getOrderGoods($order->order_id); 
                                    $goodsCount=$goodsList->count(); */

                                    $goodsList = $order->order_goods;
                                    $goodsCount = count($goodsList); 

                                ?>

			                    @foreach($goodsList as $orderGoodskey=>$orderGoods)

			                    <?php $notLast= ($orderGoodskey+1 < $goodsCount); ?>

                            	<li class="ui-border-t">
	                                <div class="ui-list-img">
	                                    <span style="background-image:url({{$pictureService::thumb($orderGoods['goods_thumb'], 70, 70)}})"></span>
	                                </div>
	                                <div class="ui-list-info">
	                                    <h4 class="ui-nowrap">{{$orderGoods['goods_name']}}</h4>
	                                    <h4><span>¥{{$orderGoods['shop_price']}}</span> ×{{$orderGoods['buy_number']}}</h4>

	                                </div>

	                            </li>
	            				@endforeach

	                        </ul>
	                    </div>
	                    <div class="ui-my-order-footer">共{{ $goodsCount }}件商品    实付款:   <font class="ui-font-red">¥{{$order->pay_price}}</font></div>
	                    <div class="ui-my-order-toolbar ui-border-t">
	                        <div class="ui-btn-wrap">

	                            @if($order->order_status==1)
		                            <button class="ui-btn-s J-cancelOrder" data-id="{{$order->order_id}}">
		                                取消
		                            </button>
		                            <button class="ui-btn-s ui-btn-danger" data-id="{{$order->order_id}}" onclick="window.location.href='/pay/request/{{$order->order_id}}/{{$pay_type}}'">
		                                支付
		                            </button>
	                            @elseif($order->order_status==2)
	                            	<button class="ui-btn-s ui-btn-default J-payagain" data-id="{{$order->order_id}}" data-goCart="true">再次购买</button>
	                            @elseif($order->order_status==3)
	                            	<button class="ui-btn-s ui-btn-default J-receive" data-id="{{$order->order_id}}">确认收货</button>

	                            @elseif($order->order_status==4)

	                            	@if(!@$request->condations['refund_goods'])

		                                @if($order->comment_status==0)
			                            <button data-href="/comment?order_id={{$order->order_id}}" class="ui-btn-s ui-btn-default" data-id="{{$order->order_id}}">
			                                评价
			                            </button>
		                                @elseif($order->comment_status==1)
		                                <button class="ui-btn-s ui-btn-gray" >已评价</button>
		                                @endif

	                                @endif
	                            	<button class="ui-btn-s ui-btn-default J-payagain" data-id="{{$order->order_id}}" data-goCart="true">再次购买</button>
	                            @elseif($order->order_status==5)
	                                <!-- <a  class="ui-btn-s ui-btn-payagain ui-btn-default" data-id="{{$order->order_id}}" data-goCart="true" href="javascript:;">再次购买</a> -->
	                            	<button class="ui-btn-s ui-btn-default J-payagain" data-id="{{$order->order_id}}" data-goCart="true">再次购买</button>
	                            @endif
	                            <button class="ui-btn-s ui-btn-default" data-id="{{$order->order_id}}" onclick="window.location.href='/my/order/{{$order->order_id}}'">
	                                订单详情
	                            </button>
	                        </div>
	                    </div>
	                </div>

	            @endforeach


            	</div>

	            <div class="J-loading ui-loading-wrap af-hide">
                    <p>正在加载中...</p>
                    <i class="ui-loading"></i>
                </div>



        @else
        <div>
            <section class="ui-placehold-wrap">
                <!-- <div class="ui-placehold">暂无记录</div> -->
                <div class="ui-placehold">无订单</div>
                <!-- <a href="/">去看看有哪些热门商品 &gt;</a> -->
            </section>

        </div>
         @endif
    </div>


</div>
<footer>

</footer>
<script type="text/html" id="tpl">
    <ul class="ui-list ui-list-pure" id="${id}">
        <li>
            <h5><span>${phone}</span><span class="date">${add_time}</span></h5>
        </li>
        <li class="af-border-t">
            <div class="ui-score"><div class="ui-score-light" style="width: ${score}%;"></div></div>
            <h6>${content}</h6>
            <ul class="ui-row ui-comments-pictures">
                ${picHtml}
            </ul>
        </li>
    </ul>
</script>

<script src="{{ asset('/build/lib/zepto.min.js') }}"></script>
<script src="{{ asset('/build/js/frozen.js') }}"></script>
<script src="{{ asset('/build/js/app.min.js') }}"></script>
<script>



	$(function(){

	var wrap=$('#listWrap');
    //取消订单
	wrap.on('click','.J-cancelOrder',function(){
		var that=$(this),id=that.attr('data-id'),reason='';
            var dialog=$.dialog({
                title:'提示信息',
                content:'确定要取消订单吗?',
                button:['取消','确认']
            });

            dialog.on('dialog:action',function(e){
                if(e.index){
                    var lk=Locked();
                    $.ajax({url:'/my/order/cancelOrder?'+Math.random(),type:'post',dataType:'json',data:{order_id:id,reason:reason},
                        success:function(res){
                            if(res.status){
                                that.closest('.ui-my-order').fadeOut(500,function(){$(this).remove()});
                                window.location.reload();
                            }else{
                                topTip(res.message||'取消失败',{type:'warn'});
                            }
                        },
                        error:function(e){
                            topTip(e.responseText||'取消失败',{type:'warn'});
                        },
                        complete:function(){
                            UnLocked(lk);
                        }
                })
                }
            });

	});
	//确认收货
	wrap.on('click','.J-receive',function(e){
	  e.preventDefault();
    		    //获取订单ID
    			var that=$(this),order_id=that.attr('data-id');
    			var dialog=$.dialog({
    		        title:'提示信息',
    		        content:'您确认已经收到货了？',
    		        button:['取消','确认']
    		    });

    		    dialog.on('dialog:action',function(e){
    		    	if(e.index){
    		    		var lk=Locked();
    		    		$.ajax({url:'/my/order/receiveGoods?'+Math.random(),type:'post',dataType:'json',data:{order_id: order_id, '_token': '{{csrf_token()}}'},
    		    			success:function(res){
    		    				if(res.status){
    		    					// that.closest('.ui-my-order').fadeOut(500,function(){$(this).remove()});
    		                		window.location.reload();
    		    				}else{
    		    					topTip(res.message||'取消失败',{type:'warn'});
    		    				}
    		    			},
    		    			error:function(e){
    		    				topTip(e.responseText||'取消失败',{type:'warn'});
    		    			},
    		    			complete:function(){
    		    				UnLocked(lk);
    		    			}
    		    		})
    		    	}
    		    });
	});

	//再次购买
	wrap.on('click','.J-payagain',function(e){
	 e.preventDefault();

    		    //获取订单ID
    			var btn = $(this), order_id = btn.attr('data-id'), goCart = btn.data('gocart');
    		    var allCartItem=$('.js-cart').find('.cart-num');

    		    if(typeof order_id=='undefined'||order_id==''||btn.hasClass('disabled')||btn.prop('disabled')) return false;
    		    btn.addClass('disabled').prop('disabled',true);
    		    $.ajax({
    		        url: '/my/order/payagain' + "?" + Math.random(),
    		        type: 'post',
    		        data: {order_id: order_id, '_token': '{{csrf_token()}}'},
    		        dataType: "json",
    		        success: function (result) {

    		           if (typeof result.goods_num!='undefined' && result.goods_num != ''){
    		               topTip('已加入购物车',{
                                   icon: 1,
                                   offset:'10px',
                                   time:2000
                                 });

    		               allCartItem.text(result.goods_num);
    		               if(goCart) window.location.href='/cart';//如果按钮带data="goCart"属性，成功后跳转到购物车
    		           } else if(typeof result.message!='undefined' && result.message != ''){
    		           console.log(result);
                           Alert(result.message);
    		           } else{
    		               topTip('操作失败，请刷新页面重试！',{type:'warn'});
    		           }
    		           btn.removeClass('disabled').prop('disabled',false);
    		        }

    		    });

	});
	//删除订单
	wrap.on('click','.J-delete',function(e){
	    var that=$(this),id=that.attr('data-id');
    			var dialog=$.dialog({
    		        title:'提示信息',
    		        content:'确定要删除吗?',
    		        button:['取消','确认']
    		    });

    		    dialog.on('dialog:action',function(e){
    		    	if(e.index){
    		    		var lk=Locked();
    		    		$.ajax({url:'/my/order/'+id,type:'delete',dataType:'json',
    		    			success:function(res){
    		    				if(res.status){
    		    					that.closest('.ui-my-order').fadeOut(500,function(){$(this).remove()});
    		    				}else{
    		    					topTip(res.message||'提交失败',{type:'warn'});
    		    				}
    		    			},
    		    			error:function(e){
    		    				topTip(e.responseText||'提交失败',{type:'warn'});
    		    			},
    		    			complete:function(){
    		    				UnLocked(lk);
    		    			}
    		    	})
    		    	}
    		    });
	});

	});
</script>
<?php $condations=$request->input('condations'); ?>
<script>
    $(function () {
        var loading=$('.J-loading');
        var listWrap=$('#listWrap');
        var ajaxData={
            page:2,
            _token:_token
        };
        scrollLoading();
        function ajaxComment(data,callback) {
data.condations={
order_status:'@if(isset($condations['order_status'])){{$condations['order_status']}} @endif',
refund_goods:'@if(isset($condations['refund_goods'])){{$condations['refund_goods']}} @endif'
};

            $.ajax({url:'/my/get_order',type:'post',dataType:'html',data:data,

                success:function (res) {
                    typeof callback==='function'&&callback(res);
                },
                error:function (error,errorType) {
                    topTip(error.message,{type:'warn'});
                }
            });
        }
        function scrollLoading() {
            var body = $('body');
            var isOver=false;
            DelCookie('scroll_loading');

            $(window).on('scroll',function () {
                var scrollTop=body.scrollTop();
                var winH=$(window).height();
                var docH=body.height();
                if(scrollTop+winH>docH-150&&!GetCookie('scroll_loading')&&!isOver){
                    //修改列表内容
                    loading.css({'display':'-webkit-box'});

                    SetCookie('scroll_loading',1);
                    ajaxData.page=listWrap.data('page')||2;
                    ajaxComment(ajaxData,function (res) {
                    var ht=$(res);
                    console.log(ht,ht.find('.ui-my-order-header').length);
                        //判断是否为最后一页  处理加载中。。。提示
                        loading.hide();
                        listWrap.append(ht);
                        listWrap.data('page',ajaxData.page+1);
                        if(ht.find('.ui-my-order-header').length<6){
                            isOver=true;
                        }
                        DelCookie('scroll_loading');
                    });
                }
            });
        }
    });

</script>
</body>
</html>